<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>
<div id="patient-bed-wrap" class="ares-pb">
    <form class="layui-form" lay-filter="patient-bed-search-from">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">患者编号</label>
                <div class="layui-input-inline">
                    <input name="search-patient-key" id="search-patient-key" placeholder="患者编号 | 姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn" lay-submit lay-filter="patient-bed-search-submit">查询</button>
            </div>
        </div>
    </form>
    <div id="patient-bed-items" class="ares-row"></div>
    <table class="layui-hide" id="patient-table" lay-filter="patient-table"></table>
    <div id="pagination-patient"></div>
</div>
<div id="patient-detail-wrap" class="layui-hide ares-clearfix">
    <button id="show-bed-btn" class="ares-float-right layui-btn layui-btn-xs layui-btn-primary">返回</button>
    <h3>基本信息</h3>
    <div class="emr-info ares-border-bottom ares-mb"></div>

    <form class="layui-form" layui-filter="patient-detail-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">时间范围</label>
                <div class="layui-input-inline">
                    <input class="layui-input ares-w-auto" readonly id="patient-detail-date" placeholder=" - ">
                </div>
            </div>
        </div>
    </form>
    <blockquote id="detail-sum-fee" class="layui-elem-quote ares-py-sm"></blockquote>
    <blockquote id="detail-evaluate-desc" class="layui-elem-quote ares-py-sm"></blockquote>
    <div id="detail-evaluate-list" class="ares-row"></div>
    <blockquote id="detail-train-desc" class="layui-elem-quote ares-py-sm"></blockquote>
    <div id="detail-train-list" class="ares-row"></div>
</div>

<script type="text/html" id="patientTab">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script>
    $(function ($) {
        var defaultStartTime = +new Date().setMonth(new Date().getMonth() - 1),
            defaultEndTime = +new Date();
        var patientId = "",
            $patientBedItems = $("#patient-bed-items"),
            $detailEvaluateDesc = $("#detail-evaluate-desc"),
            $detailEvaluateList = $("#detail-evaluate-list"),
            $detailTrainDesc = $("#detail-train-desc"),
            $detailTrainList = $("#detail-train-list");

        laydate.render({
            elem: '#patient-detail-date',
            btns: ['confirm'],
            range: true,
            value: dateFilters.dateFormat(defaultStartTime) + ' - ' + dateFilters.dateFormat(defaultEndTime),
            done: function (value, date, endDate) {
                getDetailList(patientId)
            }
        });
        form.on('submit(patient-bed-search-submit)', function (data) {
            getRelatedEMR(1, config.pagination.limit);
            return false;
        });
        $('#show-bed-btn').on('click', function () {
            $("#patient-bed-wrap").removeClass("layui-hide");
            $("#patient-detail-wrap").addClass("layui-hide");
        })
        view_sm();
        getRelatedEMR(1, config.pagination.limit);

        //小床位信息
        function view_sm() {
            var $fragment = $(document.createDocumentFragment());
            mandyServices.mandy_get_patient_charges_static(null).then(function (res) {
                var data = res.data,
                    chargesPatientDTOs = data.chargesPatientDTOs || [];
                $.each(chargesPatientDTOs, function (idx, item) {
                    var $item = $('<div class="ares-col-sm-4 ares-col-md-3 bed-item">' +
                        '<div class="ares-text-center ares-p-sm ares-rounded-sm ares-bg-whitesmoke">' +
                        '<p>' + item.name + ' 总收费: ' + item.total + '元</p>' +
                        '<p>' + item.wardBedNumber + '</p>' +
                        '</div>' +
                        '</div>');
                    $item.data('info', item);
                    $fragment.append($item);
                })
                $patientBedItems.empty().append($fragment);
            })
        }

        $patientBedItems.on('mouseenter', '.bed-item', function () {
            var info = $(this).data("info"),
                chargesStaticDTOs = info.chargesStaticDTOs || [],
                $tips = $('<div>'
                    + '<p>患者 : ' + info.name + '</p>'
                    + '<p>病历号 : ' + info.emrNumber + '</p>'
                    + '<p>病区/床位号 : ' + (info.wardBedNumber || '——') + '</p>'
                    + '<p>总收费 : ' + info.total + '元</p>'
                    + '<p>入院天数 : ' + utils.newHospitalized(info.admission_date) + '天</p>'
                    + '</div>'),
                $group = $(document.createDocumentFragment());
            $.each(chargesStaticDTOs, function (idx, item) {
                var groupName = item.groupName;
                switch (groupName) {
                    case "物理治疗组":
                        $group.append('<p>' +
                            '<b>PT组: </b>' +
                            '<span class="ares-mr">' + item.count + '次</span>' +
                            '<span>' + item.total + '元</span>' +
                            '</p>');
                        break;
                    case "作业治疗组":
                        $group.append('<p>' +
                            '<b>OT组: </b>' +
                            '<span class="ares-mr">' + item.count + '次</span>' +
                            '<span>' + item.total + '元</span>' +
                            '</p>');
                        break;
                    case "儿童治疗组":
                        $group.append('<p>' +
                            '<b>ET组: </b>' +
                            '<span class="ares-mr">' + item.count + '次</span>' +
                            '<span>' + item.total + '元</span>' +
                            '</p>');
                        break;
                    case "言语治疗组":
                        $group.append('<p>' +
                            '<b>ST组: </b>' +
                            '<span class="ares-mr">' + item.count + '次</span>' +
                            '<span>' + item.total + '元</span>' +
                            '</p>');
                        break;
                    case "理疗治疗组":
                        $group.append('<p>' +
                            '<b>LL组: </b>' +
                            '<span class="ares-mr">' + item.count + '次</span>' +
                            '<span>' + item.total + '元</span>' +
                            '</p>');
                        break;
                }
            })
            $tips.append($group);
            layer.tips($tips.html(), this, {tips: 1, time: false});
        })
        $patientBedItems.on('mouseleave', '.bed-item', function () {
            layer.closeAll('tips');
        })

        function loadDetailList(data) {
            getDetailInfo(data.id);
            getDetailList(data.patient.id)
        }

        //患者收费详情患者基本信息
        function getDetailInfo(emr_id) {
            commonServices.getEmrInfoService(emr_id, null, null, $('.emr-info'))
        }

        //患者收费详情
        function getDetailList(patient_id) {
            patientId = patient_id;
            $("#patient-bed-wrap").addClass("layui-hide");
            $("#patient-detail-wrap").removeClass("layui-hide");
            $detailEvaluateList.empty();
            $detailTrainList.empty();
            var timeRange = $('#patient-detail-date').val().split(' - ');
            mandyServices.mandy_get_patient_charges_static_detail(patient_id, +new Date(timeRange[0]), +new Date(timeRange[1])).then(function (res) {
                var data = res.data;
                $("#detail-sum-fee").text('总收费: ' + data.total);
                $.each(data.chargesDTOs, function (i, v) {
                    if (v.itemType == 1) {
                        $detailEvaluateDesc.empty().append('<span class="ares-mr-lg">康复评估 (次) : ' + v.number + '</span><span>总金额 (元) : ' + v.fee + '</span>');
                        $.each(v.chargeDetailDTOs, function (index, evaluate) {
                            $detailEvaluateList.append('<div class="ares-col-md-4 ares-col-lg-3">' +
                                '<div class="ares-rounded-sm ares-text-center ares-bg-cyan ares-text-white ares-p-sm">' +
                                '<div class="ares-text-truncate">' + evaluate.itemName + '</div>' +
                                '<div class="ares-border-bottom">' + evaluate.number + ' 次</div>' +
                                '<div>总金额 (元) : ' + evaluate.fee + '</div>' +
                                '</div>' +
                                '</div>');
                        })
                    } else if (v.itemType == 2) {
                        $detailTrainDesc.empty().append('<span class="ares-mr-lg">康复训练 (次) : ' + v.number + '</span><span>总金额 (元) : ' + v.fee + '</span>');
                        $.each(v.chargeDetailDTOs, function (idx, train) {
                            $detailTrainList.append('<div class="ares-col-md-4 ares-col-lg-3">' +
                                '<div class="ares-rounded-sm ares-text-center ares-bg-teal ares-text-white ares-p-sm">' +
                                '<div class="ares-text-truncate">' + train.itemName + '</div>' +
                                '<div class="ares-border-bottom">' + train.number + ' 次</div>' +
                                '<div>总金额 (元) : ' + train.fee + '</div>' +
                                '</div>' +
                                '</div>');
                        })
                    }
                })
            })
        }

        //患者收费患者列表
        function getRelatedEMR(currentPage, pageSize) {
            var params = {
                search_key: form.val('patient-bed-search-from')['search-patient-key']
            }
            if (params.search_key.length > 0) {
                params.hospital_id = store.session.get("hospital");
                mandyServices.mandy_get_all_emr(currentPage, pageSize, params).then(function (res) {
                    buildTable(res.data);
                });
            } else {
                mandyServices.mandy_get_my_emr(currentPage, pageSize, params).then(function (res) {
                    buildTable(res.data);
                });
            }
        }

        //患者收费构建jqGrid
        function buildTable(data) {
            laypage.render($.extend(config.pagination, {
                elem: 'pagination-patient',
                count: data.count,
                curr: data.current_page,
                jump: function (obj, first) {
                    if (!first) getRelatedEMR(obj.curr, obj.limit);
                }
            }));
            table.render({
                elem: '#patient-table',
                data: data.list,
                cellMinWidth: 80,
                cols: [[
                    {title: '患者编号', field: 'number', width: 160},
                    {title: '姓名', templet: '<span>{{d.patient.name}}</span>'},
                    {
                        title: '性别',
                        templet: function (d) {
                            return filters.sexFormat(d.patient.sex);
                        }
                    },
                    {
                        title: '年龄',
                        templet: function (d) {
                            return filters.ageFormat(d.patient.birthday);
                        }
                    },
                    {
                        title: '医保类型',
                        templet: function (d) {
                            return filters.insuranceTypeFormat(d.insurance_type);
                        }
                    },
                    {
                        title: '创建时间', width: 150,
                        templet: function (d) {
                            return dateFilters.dateTimeFormat(d.create_time);
                        }
                    },
                    {title: '操作', fixed: 'right', width: 60, templet: '#patientTab'}
                ]]
            });
            table.on('tool(patient-table)', function (obj) {
                switch (obj.event) {
                    case 'check':
                        loadDetailList(obj.data);
                        break;
                }
                ;
            });
        }

    })
</script>
</body>
</html>